package com.gwsdk.paysdk.acitvityV2.fragment;

import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gwsdk.paysdk.acitvityV2.adapter.GWCouponAdapter;
import com.gwsdk.paysdk.bean.CouponBean;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.http.process.CouponAllProcess;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.TextUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

import java.util.List;

public class GWMineCouponFragment extends GWBaseFragment implements View.OnClickListener {

    private ImageView backItem;

    private TextView button1,button2,button3;
    private View ind1,ind2,ind3;


    private List<CouponBean> listData;
    private GWCouponAdapter adapter;
    private View noDataView;
    private ProgressBar loadingView;
    private ListView listView;
    @Override
    public int setContentView() {
        return GWInflaterUtils.getLayout(getActivity(),"gw_fragment_mine_coupon");
    }

    @Override
    public void initView(View view) {
        backItem = view.findViewById(GWInflaterUtils.getControl(getActivity(),"btn_back"));
        backItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        button1 = view.findViewById(GWInflaterUtils.getControl(getActivity(),"button1"));
        button2 = view.findViewById(GWInflaterUtils.getControl(getActivity(),"button2"));
        button3 = view.findViewById(GWInflaterUtils.getControl(getActivity(),"button3"));

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        loadingView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"progress_bar"));

        noDataView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view"));

        listView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"list_view"));
        adapter = new GWCouponAdapter(getActivity(),handler,false);
        listView.setAdapter(adapter);

        getCouponList(2);
    }

    @Override
    public void getData() {

    }

    private int currentType = 0;
    public void getCouponList(int type) {
        if (currentType == type) return;
        currentType = type;
        loadingView.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
        noDataView.setVisibility(View.GONE);
        CouponAllProcess process = new CouponAllProcess();
        process.setType(type);
        process.setIsbind("0");
        process.post(handler);

    }

    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            loadingView.setVisibility(View.GONE);
            switch (msg.what) {
                case Constant.ALL_COUPON_SUCCESS:
                    listData = (List<CouponBean>) msg.obj;
                    if (listData != null && listData.size() > 0) {
                        adapter.setData(listData);
                        listView.setVisibility(View.VISIBLE);
                        noDataView.setVisibility(View.GONE);
                    } else {
                        listView.setVisibility(View.GONE);
                        noDataView.setVisibility(View.VISIBLE);
                    }
                    break;
                case Constant.ALL_COUPON_FAIL:
                    listView.setVisibility(View.GONE);
                    noDataView.setVisibility(View.VISIBLE);
                    if (!TextUtils.isEmpty(msg.obj.toString())) {
                        ToastUtil.show(getActivity(), msg.obj.toString());
                    }
                    break;
            }
        }
    };


    @Override
    public void onClick(View v) {
        if (v.getId() == button1.getId()){
            button1.setTextColor(Color.parseColor("#14B6FC"));
            button2.setTextColor(Color.parseColor("#E4E4E4"));
            button3.setTextColor(Color.parseColor("#E4E4E4"));
            getCouponList(2);
        }else if (v.getId() == button2.getId()){
            button1.setTextColor(Color.parseColor("#E4E4E4"));
            button2.setTextColor(Color.parseColor("#14B6FC"));
            button3.setTextColor(Color.parseColor("#E4E4E4"));
            getCouponList(3);
        }else if (v.getId() == button3.getId()){
            button1.setTextColor(Color.parseColor("#E4E4E4"));
            button2.setTextColor(Color.parseColor("#E4E4E4"));
            button3.setTextColor(Color.parseColor("#14B6FC"));
            getCouponList(4);
        }
    }
}
